//
//  TeacherQianBaoViewController.m
//  ZhiBoYuan
//
//  Created by coder_di on 2018/4/19.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "TeacherQianBaoViewController.h"
#import "QianBaoTableViewCell.h"
#import "MyShouYiTableViewCell.h"
#import "TiXianViewController.h"
#import "AdetailViewController.h"


@interface TeacherQianBaoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UIView *headerView;
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)UIButton *chongZhiBtn;

@property (nonatomic, strong)UIButton *teacherBtn;

@property (nonatomic, strong)UIButton *studentBtn;

@property (nonatomic, strong)UIView *leftLineView;

@property (nonatomic, strong)UIView *rightLineView;

@property (nonatomic, assign)BOOL isTiXian;

@property (nonatomic, strong)NSMutableArray *tixianDataArray;
@property (nonatomic, strong)NSMutableArray *shouyiDataArray;

@property (nonatomic, assign)int page;

@end

@implementation TeacherQianBaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的钱包";
    
    _page = 1;
    
    [self loadTixianData];
    
    [self xiaoshouAction];
    
    [self setHeaderView];
    
    [self addTableView];
    
    _isTiXian = YES;
    
    [self.tableView registerClass:[QianBaoTableViewCell class] forCellReuseIdentifier:@"QianBaoTableViewCell"];
     [self.tableView registerClass:[MyShouYiTableViewCell class] forCellReuseIdentifier:@"MyShouYiTableViewCell"];

}

-(void)xiaoshouAction{
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{@"page":[NSString stringWithFormat:@"%d",_page],
                       @"page_count":@"10"
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Course&a=teacherCourseSalesList",HTTPP] parameter:parameter sucess:^(id responseObject) {
        
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        
        if (_page == 1) {
            _shouyiDataArray = [NSMutableArray array];
        }
        
        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] intValue] == 200) {
            for (NSDictionary  *dicc in responseObject[@"data"][@"course_list"]) {
                [_shouyiDataArray addObject:dicc];
            }
            
            if (_isTiXian) {
                
            }else{
                if (_shouyiDataArray.count < 4) {
                    _tableView.mj_footer.hidden = YES;
                }else{
                    _tableView.mj_footer.hidden = NO;
                    
                }
            }
            
            [self.tableView reloadData];
        }else{
            
            [MBProgressHUD showTextOnly:self.view message:responseObject[@"msg"] delay:2.0];
        }
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
}

-(void)loadTixianData{
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{@"page":[NSString stringWithFormat:@"%d",_page],
                       @"page_count":@"10"
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Ucenter&a=incomeList",HTTPP] parameter:parameter sucess:^(id responseObject) {
        
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        
        if (_page == 1) {
            _tixianDataArray = [NSMutableArray array];
        }
        
        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] intValue] == 200) {
            for (NSDictionary  *dicc in responseObject[@"data"][@"income_list"]) {
                [_tixianDataArray addObject:dicc];
            }
            
            if (_isTiXian) {
                if (_tixianDataArray.count < 10) {
                    _tableView.mj_footer.hidden = YES;
                }else{
                    _tableView.mj_footer.hidden = NO;
                    
                }
            }
            
            [self.tableView reloadData];
        }else{
            
            [MBProgressHUD showTextOnly:self.view message:responseObject[@"msg"] delay:2.0];
        }
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

-(void)teacherAction{
    _leftLineView.hidden = NO;
    _rightLineView.hidden = YES;
    _page = 1;
    [self loadTixianData];
    [_teacherBtn setTitleColor:ZhuTiColor forState:0];
    [_studentBtn setTitleColor:[UIColor blackColor] forState:0];
    _isTiXian = YES;
    [self.tableView reloadData];
    
}

-(void)studentAction{
    _isTiXian = NO;
    _leftLineView.hidden = YES;
    _rightLineView.hidden = NO;
    _page = 1;
    [self xiaoshouAction];
    [_teacherBtn setTitleColor:[UIColor blackColor] forState:0];
    [_studentBtn setTitleColor:ZhuTiColor forState:0];
    [self.tableView reloadData];
}


-(void)setHeaderView{
    _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, 175 * KscreenHeight)];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(10 * KscreenWidth, 10 * KscreenHeight, 100 * KscreenWidth, 20 * KscreenHeight)];
    lable.text = @"我的学豆";
    [_headerView addSubview:lable];
    
    UIImageView *imageVC = [[UIImageView alloc]initWithFrame:CGRectMake(10 * KscreenWidth, 50 * KscreenHeight, 30 * KscreenWidth, 30 * KscreenHeight)];
    [imageVC setImage:[UIImage imageNamed:@"积分-1"]];
    [_headerView addSubview:imageVC];
    
    UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(50 * KscreenWidth, 55 * KscreenHeight, 100 * KscreenWidth, 20 * KscreenHeight)];
    lable1.text = [ZYUserManager userInfo].left_xuedou;
    lable1.textColor = [UIColor redColor];
    [_headerView addSubview:lable1];
    
    _chongZhiBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _chongZhiBtn.frame = CGRectMake(ScreenWidth - 80 * KscreenWidth, 50 * KscreenHeight, 60 * KscreenWidth, 30 * KscreenHeight);
    _chongZhiBtn.backgroundColor = [UIColor redColor];
    [_chongZhiBtn setTitle:@"提现" forState: 0];
    [_chongZhiBtn addTarget:self action:@selector(tixianAction) forControlEvents:(UIControlEventTouchUpInside)];
    _chongZhiBtn.layer.masksToBounds = YES;
    _chongZhiBtn.layer.cornerRadius = 3 * KscreenWidth;
    [_headerView addSubview:_chongZhiBtn];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake( 0, 95 * KscreenHeight, ScreenWidth, 0.5 * KscreenHeight)];
    line.backgroundColor = hui4Color;
    [_headerView addSubview:line];
    
    UIView *grayVieew = [[UILabel alloc]initWithFrame:CGRectMake(0 , 95.5 * KscreenHeight, ScreenWidth, 19 * KscreenHeight)];
    grayVieew.backgroundColor =hui1Color;
    [_headerView addSubview:grayVieew];
    
//    UILabel *jilulable = [[UILabel alloc]initWithFrame:CGRectMake(10 * KscreenWidth, 135 * KscreenHeight, 100 * KscreenWidth, 20 * KscreenHeight)];
//    jilulable.text = @"充值记录";
//    [_headerView addSubview:jilulable];
    
    
    _teacherBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _teacherBtn.frame = CGRectMake(0, 135 * KscreenHeight, ScreenWidth / 2, 20 * KscreenWidth);
    [_teacherBtn setTitleColor:ZhuTiColor forState:0];
    [_teacherBtn setTitle:@"我的提现" forState:0];
    [_teacherBtn addTarget:self action:@selector(teacherAction) forControlEvents:(UIControlEventTouchUpInside)];
    [_headerView addSubview:_teacherBtn];
    
    _leftLineView = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth / 2 - 80 * KscreenWidth) / 2, 135 * KscreenHeight + 37 * KscreenHeight, 80 * KscreenWidth, 3 * KscreenHeight)];
    _leftLineView.backgroundColor = ZhuTiColor;
    [_headerView addSubview:_leftLineView];
    
    _studentBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _studentBtn.frame = CGRectMake(ScreenWidth / 2, 135 * KscreenHeight, ScreenWidth / 2, 20 * KscreenWidth);
    [_studentBtn setTitleColor:[UIColor blackColor] forState:0];
    [_studentBtn setTitle:@"我的收益" forState:0];
    [_studentBtn addTarget:self action:@selector(studentAction) forControlEvents:(UIControlEventTouchUpInside)];
    [_headerView addSubview:_studentBtn];
    
    _rightLineView = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth / 2 - 80 * KscreenWidth) / 2 + ScreenWidth / 2, 135 * KscreenHeight + 37 * KscreenHeight, 80 * KscreenWidth, 3 * KscreenHeight)];
    _rightLineView.hidden = YES;
    _rightLineView.backgroundColor = ZhuTiColor;
    
    [_headerView addSubview:_rightLineView];
    
    UIView *zhongjianline = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth / 2, 135 * KscreenHeight, 1 * KscreenWidth, 30 * KscreenHeight)];
    zhongjianline.backgroundColor = hui1Color;
    [_headerView addSubview:zhongjianline];
    
    UIView *bottomline = [[UIView alloc]initWithFrame:CGRectMake(0, 175 * KscreenHeight, ScreenWidth, 1 * KscreenHeight)];
    bottomline.backgroundColor = hui2Color;
    [_headerView addSubview:bottomline];
    
}

//提现事件
-(void)tixianAction{
    TiXianViewController *tixianVC = [[TiXianViewController alloc]init];
    [self.navigationController pushViewController:tixianVC animated:YES];
    
}


-(void)addTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, Screenheight- NAVIGATION_BAR_HEIGHT ) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate        = self;
    _tableView.dataSource      = self;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    _tableView.estimatedRowHeight = 44.0f;//推测高度，必须有，可以随便写多少
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    _tableView.tableHeaderView = _headerView;
    //    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //tableView有数据时才显示分割线
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    
    
    // 下拉刷新
    _tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.page = 1;
        if (_isTiXian) {
            [self loadTixianData];
        }else{
            
            [self xiaoshouAction];
        }
        
        
    }];
    
    //         上拉加载
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _page = _page + 1;

        if (_isTiXian) {
            [self loadTixianData];
        }else{
            [self xiaoshouAction];
            
        }
        
    }];
    
    [self.view addSubview:_tableView];
}



#pragma mark - Navigation
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    
    return 0;
    
}

#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_isTiXian == YES) {
         return _tixianDataArray.count + 1;
    }else{
        
        return _shouyiDataArray.count;
    }
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_isTiXian == YES) {
        static NSString *identifity = @"QianBaoTableViewCell";
        //    MyIndentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifity];
        QianBaoTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell == nil) {
            cell = [[QianBaoTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifity];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        if (indexPath.row == 0) {
            cell.timeLable.text = @"时间";
            cell.xuedouLable.text = @"学豆";
            cell.xuedouLable.textColor = [UIColor blackColor];
            cell.backgroundColor = hui1Color;
        }else{
            cell.timeLable.text = [_tixianDataArray[indexPath.row - 1] valueForKey:@"create_time"];
            cell.xuedouLable.text = [NSString stringWithFormat:@"%@",[_tixianDataArray[indexPath.row - 1] valueForKey:@"income_price"]];
            
        }
        
        return cell;
    }else{
        static NSString *identifity = @"MyShouYiTableViewCell";
        //    MyIndentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifity];
        MyShouYiTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell == nil) {
            cell = [[MyShouYiTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifity];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.nameLable.text = [_shouyiDataArray[indexPath.row] valueForKey:@"name"];
        cell.timeLable.text = [_shouyiDataArray[indexPath.row] valueForKey:@"create_time"];
        cell.douLable.text = [NSString stringWithFormat:@"%@",[_shouyiDataArray[indexPath.row] valueForKey:@"price"]];
        [cell.imageVC sd_setImageWithURL:[NSURL URLWithString:[_shouyiDataArray[indexPath.row] valueForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@"0009"]];
        
        if ([[_shouyiDataArray[indexPath.row] valueForKey:@"is_tk"] intValue] == 1) {
            cell.tuiZuoLable.text = @"推";
        }else{
            cell.tuiZuoLable.text = @"做";
            
        }
        
        return cell;
        
    }
    

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_isTiXian == YES) {
        return 40 * KscreenHeight;
    }else{
         return 140 * KscreenHeight;
        
    }
   
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_isTiXian) {
        
    }else{
//        AdetailViewController *adetailVC = [[AdetailViewController alloc]init];
//        adetailVC.course_id = [_shouyiDataArray[indexPath.row] valueForKey:@"id"];
//        [self .navigationController pushViewController:adetailVC animated:YES];
      
        [Helper keChengDetailFromVC:self course_id:[_shouyiDataArray[indexPath.row] valueForKey:@"id"]];
        
        
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
